
var NoticeListView = Backbone.View.extend({
    el:$('.noticemain'),
    template: _.template(templateLoader['noticeList']),
    initialize:function(){
        this.i=0;
        this.cp = 1;
        this.listenTo(this.collection , 'add' , this.render);
    },
    render:function(model){
        this.i++;
        this.$el.append(this.template(model.toJSON()));
    },
    loadScroll:function(){
        var childli = $('#NoticeWrapper');
        setTimeout(function(){
            var parentPull = $('#noticeList');
            var pullDownEl = parentPull.find('.pullDown');
            var pullDownOffset = pullDownEl.get(0).offsetHeight;
            var pullUpEl = parentPull.find('.pullUp');
            var pullUpOffset = pullUpEl.get(0).offsetHeight;
            var pullDownLabel = parentPull.find('.pullDownLabel');
            var pullUpLabel = parentPull.find('.pullUpLabel');

            var myScroll = new iScroll('NoticeWrapper', {
                vScrollbar: false,
                hScrollbar: false,
                useTransition: true,
                topOffset: pullDownOffset,
                lockDirection: true,
                bounce: true,
                onRefresh: function () {
                    var now = new Date;
                    var dateText = [now.getMonth() + 1, '-', now.getDate(), ' ', now.getHours(), ':', now.getMinutes(), ':', now.getSeconds()].join('');
                    $('.timeLabel').text('数据下载于:' + dateText);
                },
                onBeforeScrollStart: function (e) {
                    e.preventDefault();
                },
                onScrollMove: function () {
                    this.maxScrollY = -40;
                    if (this.y > 5 && !$(pullDownEl).hasClass('flip')) {
                        pullDownLabel.text('松手开始刷新...');
                        $(pullDownEl).eq(0).removeClass('idle').addClass('flip');
                        this.minScrollY = -pullDownOffset;

                    } else if (this.y < 5 && $(pullDownEl).hasClass('flip')) {
                        $(pullDownEl).eq(0).removeClass('flip').addClass('idle');
                        pullDownLabel.text('下拉可以刷新');
                        this.minScrollY = -pullDownOffset;

                    } else if (this.y < (this.maxScrollY - pullUpOffset - 5) && !$(pullUpEl).hasClass('flip')) {
                        pullUpLabel.text('松手加载下一页...');
                        $(pullUpEl).eq(0).removeClass('idle').addClass('flip');
                        this.maxScrollY = this.maxScrollY;

                    } else if (this.y > (this.maxScrollY - pullUpOffset - 5) && $(pullUpEl).hasClass('flip')) {
                        pullUpLabel.text('上拉加载更多');
                        $(pullUpEl).eq(0).removeClass('flip').addClass('idle');
                        this.maxScrollY = this.maxScrollY;
                    }
                },
                onScrollEnd: function () {
                    if ($(pullDownEl).hasClass('flip')) {
                        $(pullDownEl).eq(0).removeClass('flip').addClass('idle');
                        pullDownLabel.text('下拉可以刷新');
                        $('#NoticeWrapper ul').prepend("<li class='WrapperLodding'><span>刷新中...</span></li>");
                        setTimeout(function(){
                            noticeListCollection.fetch(1,1);
                            $('.WrapperLodding').remove();
                        },2000);

                    } else if ($(pullUpEl).hasClass('flip')) {
                        pullUpLabel.text('上拉加载更多');
                        $(pullUpEl).eq(0).removeClass('flip').addClass('idle');
                        var selfCp = this.cp;
                        this.cp++;
                        $('#NoticeWrapper ul').append("<li class='WrapperLodding'><span>加载中...</span></li>");
                        childli.height(childli.height()+40);
                        setTimeout(function(){
                            noticeListCollection.fetch(selfCp,this.cp);
                           $('.WrapperLodding').remove();
                            childli.height(childli.height()-40);
                        },2000);
                    }
                }
            });
        },100);
    }
});
var noticeListView = new NoticeListView({collection:noticeListCollection});
var NoticeDetailView = Backbone.View.extend({
    el:$('#noticeDetail'),
    template: _.template(templateLoader['noticeDetail']),
    initialize:function(){
        this.listenTo(this.model , 'change' , this.render);
    },
    render:function(model){
        this.$el.html(this.template(model.toJSON()));
    }
});
var noticeDetailView = new NoticeDetailView({model:noticeDetailModel});